package net.mikoo.seals.manage.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.mikoo.seals.biz.service.ApplyServices;
import net.mikoo.seals.biz.service.VerifyService;
import net.mikoo.seals.common.WebContext;
import net.mikoo.seals.common.enums.VerifyStatusEnum;
import net.mikoo.seals.common.exception.ParametersException;
import net.mikoo.seals.common.model.PageModel;
import net.mikoo.seals.manage.web.common.BaseController;
import net.mikoo.seals.model.ApplyVerify;
import net.mikoo.seals.model.ApplyVerifyQuery;
import net.mikoo.seals.model.Upfile;

@Controller
@RequestMapping("/verify")
public class VerifyController extends BaseController {

	private static Logger logger = LoggerFactory.getLogger(VerifyController.class);
	
	@Autowired
	private VerifyService verifyService;
	
	@Autowired
	private ApplyServices applyServices;
	
	
	@RequestMapping("/list")
	public String list(ApplyVerifyQuery query, HttpServletRequest request, Model model) {
		
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		Long partyId = this.getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		
		query.setCompanyId(companyId);
		query.setPartyId(partyId);
		
		PageModel<ApplyVerify> pm = verifyService.getPageByQuery(query);
		model.addAttribute("pm", pm);
		model.addAttribute("query", query);
		return "/verify/list";
	}
	
	
	@RequestMapping("/info")
	public String info(ApplyVerifyQuery query, HttpServletRequest request, Model model) {
		Long partyId = getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setPartyId(partyId);
		query.setCompanyId(companyId);
		
		
		ApplyVerify verify = verifyService.getInfoByQuery(query);
		List<Upfile> upfiles =  applyServices.getApplyfiles(verify.getApplyId());
		
		model.addAttribute("upfiles", upfiles);
		model.addAttribute("verify", verify);
		
		
		if(!verify.getVerifyStatus().equals(VerifyStatusEnum.PENDING.code())) {
			
			query.setApplyId(verify.getApplyId());
			List<ApplyVerify> verifys =  verifyService.getListByQuery(query);
			model.addAttribute("verifys", verifys);
			
		}else {
			model.addAttribute("verifys", null);
		}
		
		return "/verify/info";
	}
	
	
	@RequestMapping("/submit")
	@ResponseBody
	public Map<String, Object> submit(ApplyVerify applyVerify, HttpServletRequest request){
		try {
			
			
			Long partyId = getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			applyVerify.setPartyId(partyId);
			applyVerify.setCompanyId(companyId);
			verifyService.verify(applyVerify);
			
			return result(SUCCESS, "提交审核数据成功");
		}catch(ParametersException pe) {
			logger.error("", pe);
			return result(ERROR, pe.getMessage());
		}catch(Exception e) {
			logger.error("系统错误", e);
			return result(ERROR, MSG_SYSTEM_ERROR);
		}
	}
	
	
	
	/**
	 * 详情
	 * @param scheduleId
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/detail")
	public String detail(Long scheduleId, HttpServletRequest request, Model model) {
		Long partyId = getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
//		Schedule schedule = verifyService.getVerifyDetail(scheduleId, partyId);
//		model.addAttribute("schedule", schedule);
		
		return "/schedule/detail";
	}
	
}
